extends RigidBody2D

var player: CharacterBody2D
var speed: float = 500.0
var anime: AnimationPlayer
var direction:Vector2
func _ready():
	$AudioStreamPlayer2D.play()
	anime = $AnimationPlayer
	if anime:
		anime.play("fly")
	gravity_scale = 0
	player = get_tree().get_first_node_in_group("player")
	if player == null:
		print("Player not found in group 'player'!")
func _process(delta):
	if direction.x>0:
		$Sprite2D.scale.x =6
	else:
		$Sprite2D.scale.x =-6
func _physics_process(delta):
	angular_velocity = 0
	if player:
		direction = (player.global_position - global_position).normalized()
		linear_velocity = direction * speed

func _on_area_2d_area_entered(area):
	if area.is_in_group('player'):
		if global_position.y> area.global_position.y:
			queue_free()
		else:
			player.global_position = Vector2(0,-600)
			queue_free() 
	if area.is_in_group('bubble'):
			area.get_parent().queue_free()
	
